﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Global;
using CE.Database;
using CE.UserAccess;
using CE.InputOut;

public partial class User_UserAccess_reportspage_access : System.Web.UI.Page
{
    string PageCode = "CXTHRUAC";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/ManagementSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            LoadUserList();
            LoadPageType();
        }
        if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
        {
            CheckPageAccess();
        }
    }

    protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        grvUserAccess.DataBind();
    }

    protected void ddlReports_SelectedIndexChanged(object sender, EventArgs e)
    {
        grvUserAccess.DataBind();
    }

    protected void grvUserAccess_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Int64 _PageId = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "PageId"));
            Int16 _OptionId = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "OptionId"));
            Int64 _UserId = Convert.ToInt64(ddlUser.Text);

            DropDownList ddlUserAccess = (DropDownList)e.Row.FindControl("ddlUserAccess");
            ddlUserAccess.Items.Clear();
            LoadAccessList(_OptionId, ddlUserAccess);
            
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable();
            SortedList _sPara = new SortedList();
            _sPara.Add("@PageId", _PageId);
            _sPara.Add("@UserId", _UserId);
            _dtResult = _db.GetDataTableBySP("sprocCXTHUserAccessReportsPagesSelectItemListByUser", _sPara);
            if (_dtResult != null && _dtResult.Rows.Count > 0)
            {
                ddlUserAccess.Text = _dtResult.Rows[0]["AccessLevel"].ToString();
            }
            else
            {
                ddlUserAccess.SelectedIndex = 0;
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (ddlUser.Text != "0")
        {
            ReportsPageAccess _reportspageaccess = new ReportsPageAccess();
            ReportsPageAccessDB _reportspageaccessdb = new ReportsPageAccessDB();
            int _count = grvUserAccess.Rows.Count;
            foreach (GridViewRow tt in grvUserAccess.Rows)
            {
                if (tt.RowType == DataControlRowType.DataRow)
                {
                    Int32 _AccessLevel = 0;
                    Label lblPageId = (Label)tt.FindControl("lblPageId");
                    DropDownList ddlUserAccess = (DropDownList)tt.FindControl("ddlUserAccess");
                    _AccessLevel = Convert.ToInt32(ddlUserAccess.Text);

                    _reportspageaccess.UserId = Convert.ToInt64(ddlUser.Text);
                    _reportspageaccess.PageId = Convert.ToInt64(lblPageId.Text.ToString());
                    _reportspageaccess.AccessLevel = _AccessLevel;
                    _reportspageaccess.Status = true;
                    _reportspageaccessdb.Insert(_reportspageaccess);
                }
            }
        }
    }

    private void LoadUserList()
    {
        ddlUser.Items.Clear();
        MyDatabase _db = new MyDatabase();
        DataTable _dtUser = new DataTable();
        SortedList _sPara = new SortedList();
        _dtUser = _db.GetDataTableBySP("sprocCXTHUserSelectItemList", _sPara);

        if (_dtUser != null && _dtUser.Rows.Count > 0)
        {
            for (int i = 0; i < _dtUser.Rows.Count; i++)
            {
                ddlUser.Items.Add(new ListItem(_dtUser.Rows[i]["Email"].ToString(), _dtUser.Rows[i]["UserId"].ToString()));
            }
        }
        else
        {
            ddlUser.Items.Clear();
            ddlUser.Items.Insert(0, new ListItem("None", "0"));
        }
    }

    private void LoadAccessList(Int16 OptionId, DropDownList ddlUserAccess)
    {
        if (OptionId == 1)
        {
            ddlUserAccess.Items.Insert(0, new ListItem("View", "100"));
            ddlUserAccess.Items.Insert(0, new ListItem("Not Accessible", "0"));
        }
        else if (OptionId == 2)
        {
            ddlUserAccess.Items.Insert(0, new ListItem("View/Export", "200"));
            ddlUserAccess.Items.Insert(0, new ListItem("View", "100"));
            ddlUserAccess.Items.Insert(0, new ListItem("Not Accessible", "0"));
        }
        else if (OptionId == 3)
        {
            ddlUserAccess.Items.Insert(0, new ListItem("View/Update", "300"));
            ddlUserAccess.Items.Insert(0, new ListItem("View", "100"));
            ddlUserAccess.Items.Insert(0, new ListItem("Not Accessible", "0"));
        }
        else if (OptionId == 4)
        {
            ddlUserAccess.Items.Insert(0, new ListItem("View/Export/Download", "400"));
            ddlUserAccess.Items.Insert(0, new ListItem("View/Export", "200"));
            ddlUserAccess.Items.Insert(0, new ListItem("View", "100"));
            ddlUserAccess.Items.Insert(0, new ListItem("Not Accessible", "0"));
        }
        else if (OptionId == 5)
        {
            ddlUserAccess.Items.Insert(0, new ListItem("View/Export/Update", "500"));
            ddlUserAccess.Items.Insert(0, new ListItem("View/Export", "200"));
            ddlUserAccess.Items.Insert(0, new ListItem("View", "100"));
            ddlUserAccess.Items.Insert(0, new ListItem("Not Accessible", "0"));
        }
    }

    public void LoadPageType()
    {
        try
        {
            DataTable dtPageType = General.GetComboItem("PageType", "PageTypeId", true);
            ddlReports.DataSource = dtPageType;
            ddlReports.DataTextField = dtPageType.Columns[0].ColumnName.ToString();
            ddlReports.DataValueField = dtPageType.Columns[1].ColumnName.ToString();
            ddlReports.DataBind();
        }
        catch (Exception ex)
        {
            CreateExceptionLog(ex,  "Page Access");
        }
    }

    private void CheckPageAccess()
    {        
        Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
        if (PageAccess == 0)
        {
            Response.Redirect("~/UserMessage.aspx?menu=0&mt=UM&ec=101");
        }
        else if (PageAccess > 0)
        {
            EnablePageAccess(PageAccess);
        }        
    }

    private void EnablePageAccess(Int64 PageAccess)
    {
        if (PageAccess == 100)
        {
            btnSubmit.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            btnSubmit.Enabled = true;
        }
    }

    private void CreateExceptionLog(Exception Ex, string strLocation)
    {
        String[] _fileContent = new String[6];
        _fileContent[0] = "Source : " + Ex.Source;
        _fileContent[1] = "Date Time : " + CE.Global.General.GetUTCDateTime().ToString();
        _fileContent[2] = "Stack Trace :" + Ex.StackTrace.ToString();
        _fileContent[3] = "Target Site : " + Ex.TargetSite.ToString();
        _fileContent[4] = "Location : " + "CE.Database.MyDatabase - " + strLocation;
        _fileContent[5] = "Message : " + Ex.Message;

        WriteExceptionLog("Ex_", _fileContent);
    }

    private void WriteExceptionLog(string strPrefixFileName, string[] strException)
    {
        try
        {
            MyIO _newFile = new MyIO();
            string _filePath = AppDomain.CurrentDomain.BaseDirectory + "Exception\\";
            if (_newFile.CheckDirectory(_filePath))
            {
                string _curMonth = CE.Global.General.GetUTCDateTime().Year.ToString("0000") + CE.Global.General.GetUTCDateTime().Month.ToString("00");

                if (!_newFile.CheckDirectory(_filePath + _curMonth))
                {
                    if (_newFile.CreateDirectory(_filePath, _curMonth))
                    {
                        _filePath += _curMonth + "\\";
                    }
                }
                else
                {
                    _filePath += _curMonth + "\\";
                }

                string _fileName = strPrefixFileName + _curMonth + CE.Global.General.GetUTCDateTime().Day.ToString("00") + CE.Global.General.GetUTCDateTime().Second.ToString("00") + CE.Global.General.GetUTCDateTime().Millisecond.ToString("00") + ".txt";

                _newFile.WriteNewFile(_filePath, _fileName, strException);
                _newFile = null;
            }
        }
        catch (Exception Ex)
        {
            Ex = null;
        }
    }

}
